<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="com.lynn.salary.util.ObjectFactory"%>
<%@page import="com.lynn.salary.entity.SystemFile"%>
<%@page import="com.lynn.salary.service.ISystemFileService"%>
<%@page import="com.lynn.salary.service.ISysArgumentService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<%!public static final String TEXT_CSS = "text/css";
	public static final String TEXT_HTML = "text/html";
	public static final String TEXT_PLAIN = "text/plain";
	public static final String TEXT_RICHTEXT = "text/richtext";
	public static final String TEXT_RTF = "text/rtf";
	public static final String TEXT_SGML = "text/sgml";
	public static final String TEXT_XML = "text/xml";

	public static final String IMAGE_CGM = "image/cgm";
	public static final String IMAGE_GIF = "image/gif";
	public static final String IMAGE_JPEG = "image/jpeg";
	public static final String IMAGE_PNG = "image/png";
	public static final String IMAGE_TIFF = "image/tiff";

	public static final String VIDIO_MPEG = "vidio_mpeg";

	public static final String MODEL_VRML = "model/vrml";
	public static final String MODEL_IGES = "model/iges";
	public static final String MODEL_MESH = "model/mesh";

	public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
	public static final String APPLICATION_JAVA = "application/java";
	public static final String APPLICATION_POSTSCRIPT = "application/postscript";
	public static final String APPLICATION_PDF = "application/pdf";
	public static final String APPLICATION_ZIP = "application/zip";
	public static final String APPLICATION_MSWORD = "application/msword";
	public static final String APPLICATION_MSEXCEL = "application/vnd.ms-excel";
	public static final String APPLICATION_XML = "application/xml";

	private static List resContList = null;
	/**   
	 * 功能：对文件类型进行初始化   
	 */
	static {

		resContList = new ArrayList();
		resContList.add(TEXT_CSS);
		resContList.add(TEXT_HTML);
		resContList.add(TEXT_PLAIN);
		resContList.add(TEXT_RICHTEXT);
		resContList.add(TEXT_RTF);
		resContList.add(TEXT_SGML);
		resContList.add(TEXT_XML);
		resContList.add(IMAGE_CGM);
		resContList.add(IMAGE_GIF);
		resContList.add(IMAGE_JPEG);
		resContList.add(IMAGE_PNG);
		resContList.add(IMAGE_TIFF);
		resContList.add(VIDIO_MPEG);
		resContList.add(MODEL_VRML);
		resContList.add(MODEL_IGES);
		resContList.add(MODEL_MESH);
		resContList.add(APPLICATION_OCTET_STREAM);
		resContList.add(APPLICATION_JAVA);
		resContList.add(APPLICATION_POSTSCRIPT);
		resContList.add(APPLICATION_PDF);
		resContList.add(APPLICATION_ZIP);
		resContList.add(APPLICATION_MSWORD);
		resContList.add(APPLICATION_MSEXCEL);
		resContList.add(APPLICATION_XML);
	}

	/**
	 * 功能：获取文件类型
	 */
	static String getContentType(String fileName) {
		String contentType = "application/x-msdownload";
		if (fileName.indexOf(".") > -1) {
			String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
			int size = resContList.size();
			String contTypeName = "";
			fileType = fileType.toLowerCase();
			for (int i = 0; i < size; i++) {
				contTypeName = (String) resContList.get(i);
				if (contTypeName.indexOf(fileType) > -1) {
					contentType = contTypeName;
					break;
				}
			}
		}
		return contentType;
	}%>
<%
	out.clear();
	out = pageContext.pushBody();
	
	ISysArgumentService sysArgumentService = (ISysArgumentService)ObjectFactory.getBean("sysArgumentService");
	ISystemFileService systemFileService = (ISystemFileService)ObjectFactory.getBean("systemFileService");
	
	String fid = request.getParameter("fid");
	String savePath = "";
	String serverPath = application.getRealPath("/");//服务器路径
	System.out.println("$$$$$$$$$$  fid="+fid);
	String rootStorePath = sysArgumentService.getValue("rootStorePath");
	SystemFile sf = systemFileService.getByPk(fid, SystemFile.class);
	
	String fileNotFoundMsg = "文件不存在或被已被管理员删除,请与管理员联系!<a href='"+basePath+"'>返回首页</a>";
	
	if(sf==null) {
		System.out.println("fid不存在:"+fid);
		//writeFile(getDefaultFile(serverPath),response);
		response.setCharacterEncoding("utf-8");
		ServletOutputStream os = response.getOutputStream();
		
		os.write(fileNotFoundMsg.getBytes("utf-8"));
		os.close();
		return;
	}
	
	savePath = sf.getSavePath();
	if(!rootStorePath.endsWith("/") && !rootStorePath.endsWith("\\")){
		rootStorePath=rootStorePath+File.separator;
	}
	savePath = rootStorePath + savePath;
	
	if(!new File(savePath).exists()) {
		System.out.println("在系统中找不到文件:"+savePath);
		response.setCharacterEncoding("utf-8");
		ServletOutputStream os = response.getOutputStream();
		
		os.write(fileNotFoundMsg.getBytes("utf-8"));
		os.close();
		return;
	}
	

	//要下载的文件路径  如："D:/fileload/xx.doc"
	String filePath = savePath;
	String fileName = sf.getFileName();
	FileInputStream fis = null; //输入流
	ServletOutputStream sos = response.getOutputStream(); //输出流 
	java.io.BufferedInputStream bis = null;
	try {
		//File file = new File(filePath, fileName);
		response.setContentType(getContentType(fileName) + "; charset=UTF-8");
		response.setHeader("Content-disposition", "attachment; filename=" + new String (fileName.getBytes("UTF-8" ), "iso8859-1" ));
		System.out.println("fileName下载:"+filePath);
//		fis = new FileInputStream(filePath + fileName); //读入文件
		fis = new FileInputStream(filePath); //读入文件
		
		bis  = new  java.io.BufferedInputStream(fis);
		java.io.BufferedOutputStream bos = new  java.io.BufferedOutputStream(sos);
		byte [] buff = new byte [2048];
		int  bytesRead = 0;
		while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
		  bos.write(buff, 0,bytesRead);
		}
		
		bos.flush();
	} catch (Throwable e) {
	} finally {
		try {
			bis.close();
			sos.close();
		} catch (Throwable e) {
		}
	}
%>


<%! 
	public void writeFile(File f,HttpServletResponse response) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		FileInputStream fis = null;
		try {
			String fileName = f.getName();
			fis = new FileInputStream(f);
			bis = new BufferedInputStream(fis);
	
			BufferedImage image = ImageIO.read(bis);
			String type= f.getName().substring(f.getName().indexOf(".")+1);
			ImageIO.write(image, type, response.getOutputStream());
			fis.close();
		}catch(Exception e) {
			System.out.println("################download2.jsp异常#################");
			e.printStackTrace();
		}
	}

	//获取默认图片文件
	public File getDefaultFile(String serverPath) {
		return new File(serverPath+"\\image_notFound.jpg");
	}
%>